package com.example.seven.mynews.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.seven.mynews.R;
import com.example.seven.mynews.data.MyDB;
import com.example.seven.mynews.data.Users;

public class RegisterActivity extends AppCompatActivity {
    private MyDB myDB;
    private EditText name;
    private EditText password1;
    private EditText password2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        myDB = new MyDB(this,"mydb");
        myDB.open();
        name = (EditText) findViewById(R.id.usernameRegister);
        password1 = (EditText) findViewById(R.id.passwordRegister1);
        password2 = (EditText) findViewById(R.id.passwordRegister2);

    }

    public void goBack(View view){
        Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
        startActivity(intent);
        finish();
    }

    public void register(View view){
        String username = name.getText().toString();
        String aPassword1 = password1.getText().toString();
        String aPassword2 = password2.getText().toString();
        if (username.isEmpty()||username.equals("")||aPassword1.isEmpty()||aPassword1.equals("")
                ||aPassword2.isEmpty()||aPassword2.equals("")){
            Toast.makeText(this,"输入框不能为空",Toast.LENGTH_SHORT).show();
        }else if (!aPassword1.equals(aPassword2)){
            Toast.makeText(this,"两次输入的密码不一致",Toast.LENGTH_SHORT).show();
        }else{
            Users aUser = new Users();
            aUser.setUserName(username);
            aUser.setPassword(aPassword2);
            myDB.insert(aUser);
            Toast.makeText(this,"注册成功，请登录",Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        myDB.close();
    }
}
